<?php

include_once($_SERVER["DOCUMENT_ROOT"]. "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/include/user/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/include/mblog/inc.php");

$g_cgival = array();
$g_show = array();
$g_pro = array();

if(! security_check::check_refer())
{
	throw new Exception("来路不明");
}

function check_cgi_pro()
{
	global $g_cgival, $g_show, $g_pro;
	cgi::both($g_cgival['t'],"t","json");
	$g_cgival['t'] = string::un_html($g_cgival['t']);

	cgi::both($g_cgival['callback'], "callback", "");
	$g_cgival['callback'] = string::un_html(string::un_script_code($g_cgival['callback']));
	
	
	cgi::both($g_cgival['id'],"id",0);
	$g_cgival['id'] = intval($g_cgival['id']);
	
	cgi::both($g_cgival['type'],"type","");
	
	cgi::both($g_cgival['topic'], "topic", "");
	$g_cgival['topic'] = string::un_html(string::un_script_code($g_cgival['topic']));
	
	
}


function get_data()
{
	global $g_cgival, $g_show, $g_pro;
	$id = $g_cgival['id'];
	$topic = trim($g_cgival['topic']);
	$user_info = new user_info();
	$is_login = $user_info->is_logined();
	if($is_login)
	{
		$user_detail_info = $user_info->get_cur_user_info();
		
		$uid = $user_detail_info["uid"];
		$user_nickname = $user_detail_info["user_nickname"];
		
		
	}
	else
	{
		
		throw new Exception("未登录");
	}	
	

	if($g_cgival['type'] == "del")
	{
		$db_mblog_cron_topic_tag = new db_mblog_cron_topic_tag();
		$topic_info = $db_mblog_cron_topic_tag->get_mblog_cron_topic_tag_item_by_id($id);
		if($topic_info["uid"] != $uid)
		{
			throw new Exception("sorry..这个话题不是你所管理的~");
		}
	
		$res = $db_mblog_cron_topic_tag->update_mblog_cron_topic_tag($id,0,0);
		
		
		
		
	}
	elseif($g_cgival['type'] == "add")
	{
		if(strlen($topic)<2)
		{
			throw new Exception("话题起码也要两个单词嘛");
		}
		if(strlen($topic)>50)
		{
			throw new Exception("我去，有这么长的话题么？");
		}
		
		$db_mblog_cron_topic_tag = new db_mblog_cron_topic_tag();
		$topic_info = $db_mblog_cron_topic_tag->get_mblog_cron_topic_tag_item_by_topic_with_del($topic);
		if($topic_info)
		{
			throw new Exception("这个话题已经被人抢先添加了，换个别的呗。。");
		}
		$count = $db_mblog_cron_topic_tag->get_mblog_cron_topic_tag_count_by_uid($uid);
		
		if($count >= 20)
		{
			throw new Exception("哥限制了20个话题，删掉个呗，不成@admin");
		}
		
		$res = $db_mblog_cron_topic_tag->insert_mblog_cron_topic_tag($topic,$uid,$user_nickname);
		
	}
	
	if(!$res)
	{
		throw new Exception("数据库操作失败");
	}
}

 
try
{
	check_cgi_pro();
	get_data();

	$g_show["error"] = "0";
	$g_show["errmsg"] = "操作成功";
}
catch (Exception $e)
{
	$g_show["error"] = "1";
	$g_show["errmsg"] = $e->getMessage();
}
show_pro();
exit();


function show_pro()
{
	global $g_cgival, $g_show, $g_pro;
	

	if($g_cgival['cb'])
	{
		header("Location:".$g_cgival['cb']."?pic_source={$g_show["pic_path"]}&pic={$g_show["pic_path1"]}&pic_name={$g_show["pic_name"]}&size={$g_show["size"]}&form_id={$g_show["form_id"]}&error={$g_show["error"]}&errormsg=".urlencode($g_show["errmsg"]));
	}
	else 
	{
		echo output::json($g_cgival["t"], $g_show, $g_cgival["callback"]);
	}
}